import {createApp} from 'vue'
import './style/style.css'
import ElementPlus from 'element-plus';

import App from './App.vue'
import './assets/css/iconfont.css'
import './assets/aichatcss/iconfont.css'
import './assets/aifont/iconfont.css'
import nlAxios from '@/base/request/nlAxios.js'

import routes from './router/index.js'
import Storage from '@/base/storage'
import ClientJs from '@/base/clientjs'
import Indicator from '@/base/Indicators.js'
import {pointMixin} from '@/base/log/pointLessLog.js'

import '@/base/fastclick.js'
import {createPinia} from 'pinia'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

import {
    $getComponent,
    $navigateBack,
    $navigateTo,
    $setData,
    $setState,
    $showOrHide,
    $stepJump,
} from '@/utils/defaultEvents.js';


const pinia = createPinia()
const app = createApp(App);

//从客户端获取用户会话信息
ClientJs.getSysInfo('getSysInfoCB');


app.use(routes);
app.use(pinia);
app.use(ElementPlus, {
    locale: zhCn,
})
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//低码内置函数
// @ts-ignore
app.config.globalProperties = {
    ...(app.config.globalProperties || {}),
    ...{
        $setData,
        $setState,
        $getComponent,
        $navigateBack,
        $stepJump,
        $showOrHide,
        $navigateTo,
    },
};


//全局挂载axios
app.config.globalProperties.$http = nlAxios;
//全局挂载closeDialog函数式组件
app.config.globalProperties.$loading = Indicator;

//storage公用方法
app.config.globalProperties.$storage = Storage;

app.mixin(pointMixin);


//从第三方页面回阿拉盯相关页面初始化axios中的信息
window.initTokenAfterBack = (_axios, uinfo) => {
    console.info('initTokenAfterBack===', uinfo);
    _axios.defaults.headers.tokenid = uinfo.tokenid;
    _axios.defaults.headers.operaterPhone = uinfo.servNumber;//add by qhuang 2021-11-24登录token改造
    _axios.defaults.headers.imei = uinfo.imei;//add by qhuang 2021-11-24登录token改造
    _axios.defaults.headers.aldregion = uinfo.region + '_' + uinfo.servNumber;//新增aldregion
    Storage.session.set('tokenId', uinfo.tokenid);
    let oldUinfo = Storage.session.get('userInfo');
    let relTelnum = oldUinfo && oldUinfo.relTelnum;//学生手机号
    if (relTelnum) {
        uinfo.relTelnum = relTelnum;
    }
    Storage.session.set('userInfo', uinfo);
}

/**
 * 页面初始化获取客户端提供的路由信息
 */
window.getSysInfoCB = (obj) => {
    let {serverUrl, userInfo} = obj;
    if (process.env.NODE_ENV === 'development') {
        //开发联调环境需要配置跨域标识
        // serverUrl = '/apiM' + serverUrl;
        // nlAxios.defaults.baseURL = serverUrl + process.env.VITE_GPT_BASE_DEEP_URL;
    }
    try {
        Storage.set('webUrl', serverUrl);
        let uinfo = userInfo && JSON.parse(userInfo);
        console.info(uinfo);
        window.initTokenAfterBack(nlAxios, uinfo);
        if (!obj.location && Storage.get('location')) {//客户端返回的位置信息为空
            //上一次的位置信息不为空，则不更新
        } else {
            Storage.set('appVersion', obj.verison);
            Storage.set('location', obj.location || 'empty');
            Storage.set('latitude', obj.latitude || 'empty');
            Storage.set('longitude', obj.longitude || 'empty');
        }
    } catch (error) {
    }
    // 挂载到window对象上
    window.Vue = app.mount('#aldv4App');
}